<?php


namespace App\Admin\Controllers;


use App\Http\Controllers\Controller;
use App\Model\Address;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class AddressController extends Controller
{
    public function index(Content $content)
    {
        $form = new Form(new Address);

        $address = Address::query()->first();

        $form->text('address', '地址')->default($address->address ?? '');

        $form->footer(function ($footer) {

            // 去掉`重置`按钮
            $footer->disableReset();

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();

        });

        $form->tools(function (Form\Tools $tools) {
            // 去掉`列表`按钮
            $tools->disableList();
            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();
        });

        $form->setTitle("收货地址管理");
        $form->setAction('/admin/address/save');

        return $content->row($form);
    }


    /**
     *
     * @param Request $request
     * @return RedirectResponse
     */
    public function save(Request $request)
    {
        if (!$address = Address::query()->first()) {
            $address = new Address();
        }
        $address->address = $request->get('address');

        if ($address->save() !== false) {
            admin_toastr("操作成功", "success", ['timeOut' => 5000]);
            return back();
        }
        admin_toastr("操作失败", "warning", ['timeOut' => 5000]);
        return back();
    }
}
